home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / sound / fftscop4.zip / SMALLFFT.C < prev    next >
C/C++ Source or Header  |  1994-05-26  |  21KB  |  909 lines

  1. #include <dos.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <alloc.h>
  6. #include <string.h>
  7. #include <mem.h>
  8. #include <math.h>
  9. #define    TWO_PI    ((double)2.0 * M_PI)
  10.  
  11. typedef unsigned char DacPalette256[256][3];
  12. unsigned char far *VGAmem;
  13. struct text_info tiGFX;
  14. struct REGPACK regp;
  15. union  REGS reg;
  16. int Linecolor=1,WritePage=0,HighSpeed=0,Triggerbuffer=999;
  17. int minX=0,minY=0,maxx=320,maxy=200;
  18.  
  19. DacPalette256 Hallo;
  20.  
  21. /* Globals */
  22.  
  23. int    samples, power;
  24. double    real[2048], imag[2048], max, min, Pegel=10,FFTmax;
  25. FILE    *fpi, *fpo;
  26. int Kanal=0,Kanalmax=15;
  27. int KanalP[]={8,8};
  28.  
  29. /* Prototypes and forward declarations */
  30.  
  31. void    fft(void);
  32. int    permute(int);
  33. double    magnitude(int);
  34. void dspwrite ( unsigned char );
  35. unsigned char dspread ( void );
  36. unsigned Buffer[2048];
  37. unsigned SBuffer[8900];
  38. unsigned char far *data;
  39. unsigned char far *aligned;
  40. unsigned char far aligned_physical;
  41.  
  42.  
  43.  
  44. /* Setvgapalette256 sets the entire 256 color palette */
  45. /* PalBuf contains RGB values for all 256 colors      */
  46. /* R,G,B values range from 0 to 63                  */
  47. /* Usage:                          */
  48. /*  DacPalette256 dac256;                  */
  49. /*                              */
  50. /* setvgapalette256(&dac256);                  */
  51. void setvgapalette256(DacPalette256 *PalBuf)
  52. {
  53.   regp.r_ax = 0x1012;
  54.   regp.r_bx = 0;
  55.   regp.r_cx = 256;
  56.   regp.r_es = FP_SEG(PalBuf);
  57.   regp.r_dx = FP_OFF(PalBuf);
  58.   intr(0x10,®p);
  59. }
  60.  
  61.  
  62.  
  63. void Grafik(int GFXmode)  // 13,      14,     17mono,   18,      19vga,  -1,3
  64. {                         //320x200, 640x200, 640x480, 640x480, 320x200, text
  65. VGAmem=(char far *)MK_FP(0xa000,0);
  66. if (GFXmode>=0)
  67.   {
  68.    gettextinfo(&tiGFX);
  69.    regp.r_ax = GFXmode;
  70.    intr(0x10,®p);
  71.   }
  72. if (GFXmode<0)
  73.   {
  74.    regp.r_ax = tiGFX.currmode;
  75.    intr(0x10,®p);
  76.   }
  77. }
  78.  
  79.  
  80.  
  81. void Putpixel(int x, int y)
  82. {
  83.  reg.h.ah = 0x0c;
  84.  reg.h.al = Linecolor;
  85.  reg.h.bh = WritePage;
  86.  reg.x.cx = x;
  87.  reg.x.dx = y;
  88.  int86(0x10,®,®);
  89. }
  90.  
  91.  
  92.  
  93. void singlecolor256(int nr,int r,int g,int b)
  94. {
  95.  reg.h.ah = 0x10;
  96.  reg.h.al = 0x10;
  97.  reg.x.bx = nr;
  98.  reg.h.ch = g;
  99.  reg.h.cl = b;
  100.  reg.h.dh = r;
  101.  int86(0x10,®,®);
  102. }
  103.  
  104.  
  105.  
  106. void singlecolor16(int nr,int r,int g,int b)
  107. {
  108.  reg.h.ah=0x10;
  109.  reg.h.al=0x07;
  110.  reg.h.bl=nr;
  111.  int86(0x10,®,®);
  112.  nr=reg.h.bh;
  113.  reg.h.ah = 0x10;
  114.  reg.h.al = 0x10;
  115.  reg.x.bx = nr;
  116.  reg.h.ch = g;
  117.  reg.h.cl = b;
  118.  reg.h.dh = r;
  119.  int86(0x10,®,®);
  120. }
  121.  
  122.  
  123.  
  124.  
  125. void MakePalette()
  126. {
  127.  int frag;
  128.  for(frag=0;frag<256;frag++)
  129.    {
  130.     if(frag<32)
  131.            {
  132.         Hallo[frag][0]=0;
  133.         Hallo[frag][1]=0;
  134.         Hallo[frag][2]=2*frag;
  135.            }
  136.     if((frag>=32)&&(frag<96))
  137.                  {
  138.                   Hallo[frag][0]=0;
  139.                   Hallo[frag][1]=(frag-32);
  140.                   Hallo[frag][2]=(95-frag);
  141.                  }
  142.     if((frag>=96)&&(frag<128))
  143.                   {
  144.                    Hallo[frag][0]=2*(frag-96);
  145.                    Hallo[frag][1]=63;
  146.                    Hallo[frag][2]=0;
  147.                   }
  148.     if((frag>=128)&&(frag<192))
  149.                    {
  150.                 Hallo[frag][0]=63;
  151.                 Hallo[frag][1]=(191-frag);
  152.                 Hallo[frag][2]=(frag-128);
  153.                    }
  154.     if(frag>=192)
  155.                 {
  156.               Hallo[frag][0]=63;
  157.           Hallo[frag][1]=0;
  158.           Hallo[frag][2]=0;
  159.           if(frag<223)Hallo[frag][2]=2*(222-frag);
  160.              }
  161.    }
  162.  setvgapalette256(&Hallo);
  163. }
  164.  
  165.  
  166. //------------------------------------------------------------------------------
  167. void dspwrite ( unsigned char c )
  168. {
  169.     while(inportb(0x022C)&0x80);
  170.     outportb(0x022C,c);
  171. }
  172. //------------------------------------------------------------------------------
  173. void sbinit ( void )
  174. {
  175.     unsigned short x;
  176.     inportb(0x022E);
  177.     outportb(0x0226,0x01);
  178.     inportb(0x0226);
  179.     inportb(0x0226);
  180.     inportb(0x0226);
  181.     inportb(0x0226);
  182.     outportb(0x0226,0x00);
  183.     for(x=0;x<100;x++)
  184.     {
  185.         if(inportb(0x022E)&0x80)
  186.         {
  187.             if(inportb(0x022A)==0xAA) break;
  188.         }
  189.     }
  190.     //if(x==100)
  191.     //{
  192.     //    printf("Sound Blaster not found at 0220h\n");
  193.     //    exit(1);
  194.     //}
  195. }
  196. //------------------------------------------------------------------------------
  197. void sbmalloc ( void )
  198. {
  199.     unsigned long physical;
  200.     data=farmalloc(80000L);
  201.     if(data==NULL)
  202.     {
  203.         printf("Memory Allocation Error\n");
  204.         exit(1);
  205.     }
  206.     physical=((unsigned long)FP_OFF(data))+(((unsigned long)FP_SEG(data))<<4);
  207.     physical+=0x0FFFFL;
  208.     physical&=0xF0000L;
  209.     aligned_physical=(physical>>16)&15;
  210.     aligned=MK_FP(((unsigned short)aligned_physical<<12)&0xF000,0);
  211. }
  212. //------------------------------------------------------------------------------
  213. void sbsettc ( unsigned char tc )
  214. // tc = time constant = 256L - (1000000UL/samples per second)
  215. {
  216.     inportb(0x022E);
  217.     dspwrite(0x40);
  218.     dspwrite(tc);
  219. }
  220. //------------------------------------------------------------------------------
  221. void sbrec ( unsigned short len )
  222. // len = number of bytes to record to unsigned char *aligned (<=65000)
  223. {
  224.     len--;
  225.     outportb(0x0A,0x05);
  226.     outportb(0x0C,0x00);
  227.     outportb(0x0B,0x45);
  228.     outportb(0x02,0);
  229.     outportb(0x02,0);
  230.     outportb(0x83,aligned_physical);
  231.     outportb(0x03,(unsigned char)(len&0xFF));
  232.     outportb(0x03,(unsigned char)((len>>8)&0xFF));
  233.     outportb(0x0A,0x01);
  234.     dspwrite(0x24);
  235.     dspwrite((unsigned char)(len&0xFF));
  236.     dspwrite((unsigned char)((len>>8)&0xFF));
  237. }
  238. //------------------------------------------------------------------------------
  239. void sbrec2 ( unsigned short len )
  240. // len = number of bytes to record to unsigned char *aligned (<=65000)
  241. {
  242.     len--;
  243.     outportb(0x0A,0x05);
  244.     outportb(0x0C,0x00);
  245.     outportb(0x0B,0x45);
  246.     outportb(0x02,0);
  247.     outportb(0x02,0);
  248.     outportb(0x83,aligned_physical);
  249.     outportb(0x03,(unsigned char)(len&0xFF));
  250.     outportb(0x03,(unsigned char)((len>>8)&0xFF));
  251.     outportb(0x0A,0x01);
  252.     dspwrite(0x48);
  253.     dspwrite((unsigned char)(len&0xFF));
  254.     dspwrite((unsigned char)((len>>8)&0xFF));
  255.     dspwrite(0x99);
  256. }
  257. //------------------------------------------------------------------------------
  258. unsigned short dmacount ( void )
  259. {
  260.     unsigned short x;
  261.     x=inportb(0x03);
  262.     x|=inportb(0x03)<<8;
  263.     if(x==0xFFFF) inportb(0x022E);
  264.     return(x);
  265. }
  266. //------------------------------------------------------------------------------
  267. void recording(int e,int f)
  268. {
  269.  int m,n=Triggerbuffer -e,o;
  270.  int Smax=0,Spos=0;
  271.  if(HighSpeed)sbrec2(Triggerbuffer); else sbrec(Triggerbuffer);
  272.  while(dmacount()!=0xFFFF);
  273.  for(m=0;m<n;m++)
  274.     {
  275.      o= *(aligned+m);
  276.      if(o>Smax)Smax=o,Spos=m;
  277.     }
  278.  if(f>0)for(m=0;m<e;m++)
  279.            {
  280.             o = *(aligned+Spos+m) - f;
  281.             if(o<0)o=0;
  282.             if(o>199)o=199;
  283.             SBuffer[m]=o;
  284.            }
  285.  else for(m=0;m<e;m++)SBuffer[m] = *(aligned+Spos+m) - f;
  286. }
  287.  
  288.  
  289.  
  290. void lineX00(int x,int y1,int y2)
  291. {
  292.  int dy=y1-y2;
  293.  int m=x-1,v,w=abs(dy);
  294.  if(w<2)
  295.         {
  296.          *(VGAmem+((x-1)>>3)+80*y1)&=255-(1<<(7-((x-1)&7)));
  297.          *(VGAmem+(x>>3)+80*y2)&=255-(1<<(7-(x&7)));
  298.          return;
  299.         }
  300.  if(dy<0)
  301.          {
  302.           dy=w;
  303.           for(v=y1;v<=y2;v++)
  304.              {
  305.               w=v-y1;
  306.               w /=dy;
  307.               *(VGAmem+((m+w)>>3)+80*v)&=255-(1<<(7-((m+w)&7)));
  308.              }
  309.          }
  310.  else
  311.      {
  312.       for(v=y2;v<=y1;v++)
  313.          {
  314.           w=v-y2;
  315.           w /=dy;
  316.           *(VGAmem+((x-w)>>3)+80*v)&=255-(1<<(7-((x-w)&7)));
  317.          }
  318.      }
  319. }
  320.  
  321.  
  322.  
  323. void lineX01(int x,int y1,int y2)
  324. {
  325.  int dy=y1-y2;
  326.  int m=x-1,v,w=abs(dy);
  327.  if(w<2)
  328.         {
  329.          *(VGAmem+((x-1)>>3)+80*y1)|=1<<(7-((x-1)&7));
  330.          *(VGAmem+(x>>3)+80*y2)|=1<<(7-(x&7));
  331.          return;
  332.         }
  333.  if(dy<0)
  334.          {
  335.           dy=w;
  336.           for(v=y1;v<=y2;v++)
  337.              {
  338.               w=v-y1;
  339.               w /=dy;
  340.               *(VGAmem+((m+w)>>3)+80*v)|=1<<(7-((m+w)&7));
  341.              }
  342.          }
  343.  else
  344.      {
  345.       for(v=y2;v<=y1;v++)
  346.          {
  347.           w=v-y2;
  348.           w /=dy;
  349.           *(VGAmem+((x-w)>>3)+80*v)|=1<<(7-((x-w)&7));
  350.          }
  351.      }
  352. }
  353.  
  354.  
  355.  
  356. int cool0()
  357. {
  358. unsigned t;
  359. int i,m=Triggerbuffer;
  360. Triggerb